﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Audio;

namespace TopShooter
{
    public class Portal : Tile
    {
		 public int ID;
        public PortalTarget Definition;

		 static private SpriteFont _editorFont = Game.Instance.Content.Load<SpriteFont>("Fonts/Hud");

        public Portal(int id, string textureName, SortedList<int, PortalTarget> list)
            : base(textureName, TileCollision.Blocked)
        {
			  ID = id;
            MapCode = id.ToString()[0];
            if (list != null)
            {
                foreach (var pair in list)
                {
                    if (pair.Key == id)
                    {
							  PortalTarget def = pair.Value;
                        Definition = def;
                        if (def.Direction == 'A' || def.Direction == 'E')
                            TextureName = "Tiles/Portal_2";
                        break;
                    }
                }
            }
            if (Definition == null)
            {
                Definition = new PortalTarget("0,0,W");
            }
        }

		  public override void OnAdding(Scene scene, Coordinate loc)
		  {
			  for (int r = 0; r < scene.Height; r++)
			  {
				  for (int c = 0; c < scene.Width; c++)
				  {
                      Coordinate coord = new Coordinate(c, r);
					  Portal p = scene.GetTileAtCoordinate(coord) as Portal;
					  if (p != null)
					  {
						  if (p.ID == ID)
						  {	// found our twin, let's just move it
							  scene.SetTile(Assets.Tiles.Floor, coord);
							  Definition = p.Definition;
						  }
					  }
				  }
			  }
		  }

		  public override void OnEntityCollided(Entity entity)
        {
			  //if (player is PlayerEntity)
			  //{
			  //   PlayerEntity p = player as PlayerEntity;
			  //   if (p.IsUserControlled && Definition != null)
			  //   {
			  //      base.OnEntityCollided(player);
			  //      Scene.Instance.ReachedPortal = Definition;
			  //   }
			  //}
        }
        public override string SetDefinition(string def)
        {
            string oldDef = GetDefinition();
            if (string.IsNullOrEmpty(def))
                return oldDef;
            Definition = new PortalTarget(def);
            return oldDef;
        }
        public override string GetDefinition()
        {
            return Definition.GetDataString();
        }

		  public override void Draw(SpriteBatch spriteBatch, Vector2 offset, int column, int row)
		  {
			  if (Game.IsEditorMode)
			  {
				  float x = -Scene.Instance.VportOffset.X + (column * Tile.Size.X) + 4;
				  float y = -Scene.Instance.VportOffset.Y + (row * Tile.Size.Y);

				  Game.Instance.DrawShadowedString(_editorFont, ID.ToString(), new Vector2(x, y), Color.Black);
			  }
			  else
			  {
				  base.Draw(spriteBatch, offset, column, row);
			  }
		  }


        public class PortalTarget
        {
            public string SceneName;
            public int Portal;
            public char Direction;
            public bool IsCheckPoint;

            public PortalTarget(string data)
            {
                string[] tokens = data.Split(',');
                SceneName = tokens[0];
                Portal = int.Parse(tokens[1]);
                Direction = tokens[2][0];

                if (tokens.Length > 3)
                {
                    if (tokens[3] == "Y")
                        IsCheckPoint = true;
                    
                }
            }
            public PortalTarget(string Scene, int portal)
            {
                SceneName = Scene;
                Portal = portal;
            }
            public override string ToString()
            {
                return "PD[" + SceneName + ", " + Portal + ", " + IsCheckPoint + "]";
            }
            public string GetDataString()
            {
                return SceneName + "," + Portal + "," + Direction; 
            }

        }
    }

}
